<?php
include_once("./configure/configure.php");						//--> global var
include_once("./admin/configure/admin.config.inc.php");			//-- admin var
include_once("./admin/tools/smarty/libs/Smarty.class.php");		//--> out template

include_once("impl/CouponImpl.php");
include_once("impl/CouponAttributeImpl.php");

include_once("impl/CategoryImpl.php");
include_once("impl/MerchantImpl.php");

include_once("./user/checkLogin.php");// check login status
include_once("includeSmarty.php");

$id		= 0;
$page 	= $_POST['page'];

if(empty($page))
	$page = 1;
	
$cacheId = $page;

if(!$smarty->isCached('index.html', $cacheId, $cacheId)){	
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	$oCAttributeImpl= new CouponAttributeImpl(DB_TAG_COUPON);
	
	$oMerchantImpl 	= new MerchantImpl(DB_TAG_COUPON);
	$oCategoryImpl	= new CategoryImpl(DB_TAG_COUPON);
	
	include_once("includeTag.php");
	include_once("includeCategories.php");
	
	
	$where 		= " WHERE status = 1 AND (isRecommend = 1 OR isRecommend = 2)";
	$orderStr	= " ORDER BY r_sortValue DESC";
	
	$oRecommendedCoupons = $oCouponImpl->getCustoms($where, $orderStr, $page, 5);	
	foreach ($oRecommendedCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
		
		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
			
		$oCoupon->setCategory($oCategory);
		
		$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}

		if($oCoupon->getCreativeType() == 2){
			$oLogo = $oMerchantImpl->getLogo($oCoupon->getMerchantId());
			
			if(!is_object($oLogo)){
				$oLogo = new ImageDomain();
				
				$oLogo->setId(0);
				$oLogo->setExtName('gif');	
			}
			
			$oMerchant->setLogo($oLogo);
		}			
		
		$oCoupon->setMerchant($oMerchant);
	}
	
	
	$where 		= " WHERE status = 1 AND isFeatured = 1";
	$orderStr	= " ORDER BY r_sortValue DESC";
	
	$oCoupons = $oCouponImpl->getCustoms($where, $orderStr, $page, 15);
	
	foreach ($oCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
		
		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
			
		$oCoupon->setCategory($oCategory);
		
		$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}

		if($oCoupon->getCreativeType() == 2){
			$oLogo = $oMerchantImpl->getLogo($oCoupon->getMerchantId());
			
			if(!is_object($oLogo)){
				$oLogo = new ImageDomain();
				
				$oLogo->setId(0);
				$oLogo->setExtName('gif');	
			}
			
			$oMerchant->setLogo($oLogo);
		}			
		
		$oCoupon->setMerchant($oMerchant);
	}
	
	
	$smarty->assign('oCatTree', $oCatTree);
	$smarty->assign('oTags', $oTags);
	$smarty->assign('oCoupons', $oCoupons);
	$smarty->assign('oRecommendedCoupons', $oRecommendedCoupons);
	
	
	$smarty->assign('pageList', $oCouponImpl->getPageList(5));
	$smarty->assign('nextPage', $oCouponImpl->getNextPage());
	$smarty->assign('prePage', $oCouponImpl->getPrePage());
	$smarty->assign('pageCount', $oCouponImpl->getPageCount());
	$smarty->assign('page', $oCouponImpl->getPage());
	
	$smarty->assign('id', $id);
	
}

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);
$smarty->assign('DEBUG', DEBUG);
$smarty->assign('userId', $userId);


$smarty->display('index.html', $cacheId, $cacheId);
?>
